﻿using System;
using System.Threading;
using TYBase;
using TYRTUBase.Comm;

namespace TYRTUBase
{
    public class Device_IR_3561 : Device_IR
    {
        public Device_IR_3561(string equipId, string equipName) : base(equipId, equipName)
        {
            ReadOutTime = 2000;
            SetPara(new ScanerPara
            {
                baudRate = 9600,
                dataBits = 8,
                NewLine = new byte[] { 0x0d, 0x0a },
                parity = System.IO.Ports.Parity.None,
                stopBits = System.IO.Ports.StopBits.One,
            });
        }


        public override bool DataToIR(string data, out double ocv, out double ir)
        {
            ocv = 0;
            ir = 0;
            if (data.Null())
                return true;
            string[] array = data.Replace(" ", "").Split(',');
            if (array.Length == 2)
            {
                ocv = Math.Round(Math.Abs(array[1].AsDouble()), 5);
                ir = Math.Round(Math.Abs(array[0].AsDouble() * 1000.0), 2);
                return true;
            }
            return false;
        }

        public override bool InitIR(EnumIRMeasSpd irSpd)
        {
            Log.I("开始初始化IR串口");
            Spd = irSpd;
            if (IsXModerMode)
            {
                return true;
            }
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }
            return true;
        }
        public bool SendRead()
        {
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }
            Log.I("内阻仪:开始读取数据");
            string sendCmd = ":READ?";
            rtu.Write(ToCmdByte(sendCmd));
            return true;
        }

        public override bool ReadData(out string data)
        {
            if (IsXModerMode)
            {
                double ocv = _rand.Next((int)(XMode_IR_Para.OCVMin * 10000), (int)(XMode_IR_Para.OCVMax * 10000)) * 1.0d / 10000;
                double ir = _rand.Next((int)(XMode_IR_Para.IRMin * 10000), (int)(XMode_IR_Para.IRMax * 10000)) * 1.0d / 10000;
                data = IRDataHelper.OCVIR_2_String(ocv, ir);
                return true;
            }
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                data = "";
                return false;
            }
            string sendCmd = "FETCh?";
            rtu.ClearInBuff();
            rtu.Write(ToCmdByte(sendCmd));

            DateTime dt = DateTime.Now;
            byte[] dataRead = null;
            while (true)
            {
                if (IsRun == false)
                {
                    data = "";
                    return false;
                }
                if (DateTime.Now - dt > TimeSpan.FromMilliseconds(ReadOutTime))
                {
                    Log.E($"内阻仪:读取数据超时:{dataRead.AsString()}");
                    data = "";
                    return false;
                }
                Thread.Sleep(10);
                if (rtu.Read(out byte[] dataTmp) > 0)
                {
                    dataRead = dataRead.Append(dataTmp);
                    if (dataRead != null && dataRead.Length > 0)
                    {
                        var listbytes = dataRead.Split(NewLine);
                        if (listbytes != null && listbytes.Count > 0)
                        {
                            var lastData = listbytes[listbytes.Count - 1];
                            var index = lastData.Indexof(NewLine);
                            if (index > 0)
                            {
                                byte[] dataget = new byte[index];
                                Array.Copy(lastData, dataget, index);
                                data = dataget.AsString();
                                return true;
                            }
                        }
                    }
                }
                else { continue; }
            }
        }
    }
}